package com.alan.camunda.boot.config;

import com.alan.camunda.boot.util.ExpressionManagerHolder;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.cfg.ProcessEnginePlugin;
import org.camunda.bpm.engine.impl.el.ExpressionManager;
import org.camunda.bpm.engine.impl.incident.IncidentHandler;
import org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor;
import org.camunda.bpm.engine.impl.interceptor.CommandInterceptor;
import org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor;
import org.camunda.bpm.engine.spring.SpringTransactionInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class MyEnginePlugin implements ProcessEnginePlugin {
    @Resource
    private PlatformTransactionManager transactionManager;

    @Override
    public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {

    }

    @Override
    public void postInit(ProcessEngineConfigurationImpl configuration) {
        log.info("MyEnginePlugin postInit");
        //ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
        //ExpressionManagerHolder.set(expressionManager);

        List<CommandInterceptor> txItems = configuration.getCommandInterceptorsTxRequired();
        // 确保指定位置是预期的 CommandInterceptor
        Assert.isTrue(txItems.get(txItems.size() -3) instanceof ProcessApplicationContextInterceptor, "事务控制器不是预期的类型");
        Assert.isTrue(txItems.get(txItems.size() -2) instanceof CommandContextInterceptor, "事务控制器不是预期的类型");

        SpringTransactionInterceptor txList = new SpringTransactionInterceptor(transactionManager, TransactionTemplate.PROPAGATION_REQUIRED, configuration);
        txItems.get(txItems.size() -3).setNext(txList);
        txList.setNext(txItems.get(txItems.size() -2));
        txItems.add(txItems.size() -2, txList);

        List<CommandInterceptor> txNewItems = configuration.getCommandInterceptorsTxRequiresNew();
        // 确保指定位置是预期的 CommandInterceptor
        Assert.isTrue(txNewItems.get(txNewItems.size() -3) instanceof ProcessApplicationContextInterceptor, "事务控制器不是预期的类型");
        Assert.isTrue(txNewItems.get(txNewItems.size() -2) instanceof CommandContextInterceptor, "事务控制器不是预期的类型");
        SpringTransactionInterceptor txNewItem = new SpringTransactionInterceptor(transactionManager, TransactionTemplate.PROPAGATION_REQUIRES_NEW, configuration);
        txNewItems.get(txNewItems.size() -3).setNext(txNewItem);
        txNewItem.setNext(txNewItems.get(txNewItems.size() -2));
        txNewItems.add(txNewItems.size() -2, txNewItem);

        System.out.println("已调整 CommandInterceptorsTxRequired、CommandInterceptorsTxRequiresNew 列表");

    }

    @Override
    public void postProcessEngineBuild(ProcessEngine processEngine) {
        log.info("MyEnginePlugin postProcessEngineBuild");
    }


}
